package com.yanqu.road.server.manager.activity.mammonhall;

import com.yanqu.road.entity.activity.mammonhall.*;
import com.yanqu.road.entity.activity.mammonhall.enums.MammonHallRankType;
import com.yanqu.road.logic.bussiness.activity.MammonHallBusiness;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.MammonHallProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.*;

public class CrossMammonHallMgr extends TempMgr {

    private static Logger logger = LogManager.getLogger(CrossMammonHallMgr.class.getName());

    static Map<Integer, List<MammonHallSourceDataInfo>> sourceDataMap;

    public static int beginTime = 0;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        sourceDataMap = MammonHallBusiness.getRankList();
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static Map<Integer, List<MammonHallSourceDataInfo>> getSourceDataMap() {
        return sourceDataMap;
    }

    /**
     * 拉取赚钱榜单-最高店铺赚钱详情
     */
    public static void syncOtherValueData() {
        if(beginTime == 0 || DateHelper.getCurrentSecond() < beginTime){
            return;
        }
        for (Map.Entry<Integer, List<MammonHallSourceDataInfo>> listEntry : sourceDataMap.entrySet()) {
            int type = listEntry.getKey();
            if(type != MammonHallRankType.EarnSpeed.getIntValue()){
                continue;
            }
            Map<Long, List<Long>> serverUserMap = new HashMap<>();
            for (MammonHallSourceDataInfo dataInfo : listEntry.getValue()) {
                if(dataInfo.getOtherString().size() == 0){
                    if(!serverUserMap.containsKey(dataInfo.getServerId())){
                        serverUserMap.put(dataInfo.getServerId(), new ArrayList<>());
                    }
                    serverUserMap.get(dataInfo.getServerId()).add(dataInfo.getUserId());
                }
            }
            for (Map.Entry<Long, List<Long>> entry : serverUserMap.entrySet()) {
                MammonHallProto.CrossMammonHallSyncDataReqMsg.Builder syncMsg = MammonHallProto.CrossMammonHallSyncDataReqMsg.newBuilder();
                syncMsg.setType(type);
                syncMsg.addAllUserId(entry.getValue());
                getLogger().info("syncOtherValueData type {}，serverId {} userId {}", type, entry.getKey(), entry.getValue());
                MessageHelper.sendPacket(entry.getKey(), 0, YanQuMessageUtils.buildMessage(Protocol.S_MAMMON_HALL_REQ_DATA_FROM_CROSS, syncMsg));
            }
        }
    }

    /**
     * 拉取玩家形象及名字
     */
    public static void syncUserBaseInfo() {
        Map<Long, Set<Long>> serverUserMap = new HashMap<>();
        for (Map.Entry<Integer, List<MammonHallSourceDataInfo>> listEntry : sourceDataMap.entrySet()) {
            for (MammonHallSourceDataInfo dataInfo : listEntry.getValue()) {
                if(!serverUserMap.containsKey(dataInfo.getServerId())){
                    serverUserMap.put(dataInfo.getServerId(), new HashSet<>());
                }
                serverUserMap.get(dataInfo.getServerId()).add(dataInfo.getUserId());
            }
        }
        for (Map.Entry<Long, Set<Long>> entry : serverUserMap.entrySet()) {
            MammonHallProto.CrossMammonHallSyncUserBaseInfoReqMsg.Builder syncMsg = MammonHallProto.CrossMammonHallSyncUserBaseInfoReqMsg.newBuilder();
            syncMsg.addAllUserId(entry.getValue());
            getLogger().info("syncUserBaseInfo server {}， userId {}", entry.getKey(), entry.getValue());
            MessageHelper.sendPacket(entry.getKey(), 0, YanQuMessageUtils.buildMessage(Protocol.S_MAMMON_HALL_REQ_USER_DATA_FROM_CROSS, syncMsg));
        }
    }

    public static MammonHallProto.MammonHallRankTemp.Builder getRankTempBuilder(MammonHallSourceDataInfo dataInfo) {
        MammonHallProto.MammonHallRankTemp.Builder topBuilder = MammonHallProto.MammonHallRankTemp.newBuilder();
        topBuilder.setType(dataInfo.getType());
        topBuilder.setValue(dataInfo.getValue());
        topBuilder.setUserId(dataInfo.getUserId());
        topBuilder.setTime(dataInfo.getDataTime());
        topBuilder.addAllOtherValue(dataInfo.getOtherString());
        if(dataInfo.getUserBaseInfo() != null) {
            topBuilder.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(dataInfo.getUserBaseInfo()));
        }
        for (MammonHallSourceKV sourceKV : dataInfo.getKvList()) {
            MammonHallProto.MammonHallRepeatedValueTemp.Builder builder = MammonHallProto.MammonHallRepeatedValueTemp.newBuilder();
            builder.setKey(sourceKV.getItem());
            builder.setValue(sourceKV.getCount());
            topBuilder.addRepeatedVal(builder);
        }
        return topBuilder;
    }
}
